﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDDMNhaSanXuat
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deid;
        private int _istt;
        private string _sngaycn;
        private string _sSchema;
        private string _sten;

        public dtoDDMNhaSanXuat()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDDMNhaSanXuat(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMNhaSanXuat.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1 and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMNhaSanXuat.cs");
                return false;
            }
        }

        public long GetIDInsert()
        {
            long iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDDMNhaSanXuat.cs");
                iDInsert = 1L;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public long GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                long num = 0L;
                try
                {
                    num = long.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return (num += 1L);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDDMNhaSanXuat.cs");
                return 1L;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMNhaSanXuat.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnTEN, ",", columnSTT, ") values(", this._deid, ",:", columnTEN, ",", this._istt, 
                    ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMNhaSanXuat.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._sten = "";
            this._istt = 0;
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDDMNhaSanXuat.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnTEN, ",", columnSTT, ",to_char(", columnNGAYCN, ",'dd/mm/yyyy hh24:mi:ss') as ", columnNGAYCN, " from ", this._sSchema, ".", tableName, " where ", columnID, 
                    " = ", this._deid
                 }), connect).Fill(dataTable);
                this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                this._sten = dataTable.Rows[0][columnTEN].ToString();
                this._istt = int.Parse(dataTable.Rows[0][columnSTT].ToString());
                this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDDMNhaSanXuat.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDDMNhaSanXuat.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnTEN + "," + columnSTT + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + " from " + this._sSchema + "." + tableName + " order by " + columnTEN, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDDMNhaSanXuat.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMNhaSanXuat.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnTEN, " = :", columnTEN, ",", columnSTT, " = ", this._istt, ",", columnNGAYCN, " = now()  where ", columnID, 
                    " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMNhaSanXuat.cs");
                return false;
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSTT
        {
            get
            {
                return "stt";
            }
        }

        public static string columnTEN
        {
            get
            {
                return "ten";
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public string pNGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public int pSTT
        {
            get
            {
                return this._istt;
            }
            set
            {
                this._istt = value;
            }
        }

        public string pTEN
        {
            get
            {
                return this._sten;
            }
            set
            {
                this._sten = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_dmnhasanxuat";
            }
        }
    }
}

